﻿using System.Collections;
using Estsh.Core.Util;
//using Estsh.Core.Repository.IRepositories;
//using Estsh.Core.Services.IServices;
using Estsh.Core.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Dapper;
using Estsh.Core.Model.ExcelModel;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Wms.IRepositories;

/***************************************************************************************************
 *
 * 更新人：sitong.dong
 * 描述：
 * 修改时间：20223.03.09
 * 修改日志：系统迭代升级
 *
 **************************************************************************************************/
namespace Estsh.Core.Wms.Services
{
    
    /// <summary>
    /// 菜单业务处理类
    /// </summary>
    public class WorkOrderService : BaseService<MesWorkOrder_Extend>, IWorkOrderService
    {

        private readonly IWorkOrderRepository repository;

        public WorkOrderService(IWorkOrderRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        /// <summary>
        /// 产线
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> GetResourceCode(int factoryId)
        {
            return repository.GetResourceCode(factoryId);
        }
        /// <summary>
        /// 工单类型
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> GetOrderType()
        {
            return repository.GetOrderType();
        }

        /// <summary>
        /// 工单状态
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> GetOrderStatus()
        {
            return repository.GetOrderStatus();
        }

        /// <summary>
        /// 备料类型
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> GetPrepareMateria()
        {
            return repository.GetPrepareMateria();
        }

        /// <summary>
        /// 仓库
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> GetWarehouse()
        {
            return repository.GetWarehouse();
        }

        /// <summary>
        ///  根据分页条件获取分页菜单数据
        /// </summary>
        /// <param name="workCode">工单号</param>
        /// <param name="partNo">物料号</param>
        /// <param name="refWrokOrderNo">顶层订单号</param>
        /// <param name="resourceCode">工作中心编码</param>
        /// <param name="orderType">工单类型</param>
        /// <param name="orderStatus">工单状态</param>
        /// <param name="ProduceStartTimeStart">开始时间</param>
        /// <param name="ProduceStartTimeEnd">开始时间</param>
        /// <param name="ProduceEndTimeStart">结束时间</param>
        /// <param name="ProduceEndTimeEnd">结束时间</param>
        /// <param name="factoryId">工厂id</param>
        /// <param name="pager"></param>
        /// <param name="direction"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public Hashtable getWorkOrderListPage(string orderNo, string partNo, string refWrokOrderNo, string resourceCode, string orderType, string orderStatus, string ProduceStartTimeStart, string ProduceStartTimeEnd, string ProduceEndTimeStart, string ProduceEndTimeEnd,int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }

            if (partNo != null && !partNo.Trim().Equals(""))
            {
                strWhere += "and a.part_no ='" + partNo.Trim() + "' ";
            }

            if (refWrokOrderNo != null && !refWrokOrderNo.Trim().Equals(""))
            {
                strWhere += "and a.ref_work_order_no ='" + refWrokOrderNo.Trim() + "' ";
            }
            if (resourceCode != null && !resourceCode.Trim().Equals(""))
            {
                strWhere += "and a.pdline_code ='" + resourceCode.Trim() + "' ";
            }
            if (orderType != null && !orderType.Trim().Equals(""))
            {
                strWhere += "and a.order_type ='" + orderType.Trim() + "' ";
            }
            if (orderStatus != null && !orderStatus.Trim().Equals(""))
            {
                strWhere += "and a.order_status ='" + orderStatus.Trim() + "' ";

            }
            if (ProduceStartTimeStart != null && !ProduceStartTimeStart.Trim().Equals(""))
            {
                strWhere += "and a.produce_start_time >='" + ProduceStartTimeStart.Trim() + "' ";
            }
            if (ProduceStartTimeEnd != null && !ProduceStartTimeEnd.Trim().Equals(""))
            {
                strWhere += "and a.produce_start_time <='" + ProduceStartTimeEnd.Trim() + "' ";
            }
            if (ProduceEndTimeStart != null && !ProduceEndTimeStart.Trim().Equals(""))
            {
                strWhere += "and a.produce_end_time >='" + ProduceEndTimeStart.Trim() + "' ";
            }
            if (ProduceEndTimeEnd != null && !ProduceEndTimeEnd.Trim().Equals(""))
            {
                strWhere += "and a.produce_end_time <='" + ProduceEndTimeEnd.Trim() + "' ";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsPurchase).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsPurchase).GetEntityColumnName("orderNo") + " " + direction;
            }

            return repository.getPurchaseListByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);
        }




        public int saveGenerateIngredients(string prepareMaterial, string orderNos, string Warehouse, int empId)
        {
            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return repository.saveGenerateIngredients(prepareMaterial, orderNos, Warehouse, empId);
        }

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="orderNos"></param>
        /// <returns></returns>
        public int FiringData(String orderNos)
        {
            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return this.repository.FiringData(orderNos);
        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="orderNos"></param>
        /// <returns></returns>
        public int SuspendData(String orderNos)
        {
            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return this.repository.SuspendData(orderNos);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="orderNos"></param>
        /// <returns></returns>
        public int CancelData(String orderNos)
        {
            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return this.repository.CancelData(orderNos);

        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="orderNos"></param>
        /// <returns></returns>
        public int CloseData(String orderNos)
        {

            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return this.repository.CloseData(orderNos);

        }

        /// <summary>
        /// 完成
        /// </summary>
        /// <param name="orderNos"></param>
        /// <returns></returns>
        public int AccomplishData(String orderNos)
        {

            orderNos = orderNos.Substring(0, orderNos.Length - 1);
            return this.repository.AccomplishData(orderNos);

        }




        /*
        /// <summary>
        /// 保存菜单数据
        /// </summary>
        /// <param name="htParams"></param>
        /// <returns></returns>
        public int saveAdjustOrder(SysPartCustOrder htParams)
        {
            return repository.saveAdjustOrder(htParams);
        }
        /// <summary>
        /// 更新菜单数据
        /// </summary>
        /// <param name="htParams"></param>
        /// <returns></returns>
        public int updateAdjustOrder(SysPartCustOrder htParams)
        {
            return repository.updateAdjustOrder(htParams);
        }

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="ruid"></param>
        /// <returns></returns>
        public List<SysPartCustOrder> getAdjustOrder(String part_id)
        {
            part_id = "part_id = " + part_id;
            return repository.getList(part_id, "");
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int deleteAdjustOrder(String ids)
        {
            String[] idArray = ids.Split(',');
            int count = 0;
            foreach (String id in idArray)
            {
                if (!"".Equals(id))
                {
                    count += this.repository.deleteAdjustOrder(id);
                }
            }
            return count;
        }

        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool upRow(int seq, int ruid, int up_ruid, int up_seq)
        {
            return this.repository.upRow(seq, ruid, up_ruid, up_seq);
        }

        /// <summary>
        /// 上移多行
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool upAll(int ruid, int seq, int up_ruid, int up_seq)
        {
            return this.repository.upAll(ruid, seq, up_ruid, up_seq);
        }

        /// <summary>
        /// 获取上一条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<GWorkorderDetail> selectAll(string CustPDLine)
        {
            return this.repository.selectAll(CustPDLine);
        }

        /// <summary>
        /// 获取下拉框中的菜单数据
        /// </summary>
        /// <returns></returns>
        public List<KeyValueResult> getSelectAdjustOrder()
        {
            return repository.getSelectAdjustOrder();
        }

        public int deleteOrder(String ruid)
        {
            return repository.deleteOrder(ruid);
        }

        public int deleteOrderInfo(String ruid, string custPdlineDesc)
        {
            string partLocation = repository.getPartLocation(ruid);
            return repository.deleteOrderInfo(ruid, custPdlineDesc, partLocation);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int EnableData(String ids)
        {
            String[] idArray = ids.Split(',');
            int count = 0;
            foreach (String id in idArray)
            {
                if (!"".Equals(id))
                {
                    count += this.repository.EnableData(id);
                }
            }
            return count;
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DisableData(String ids)
        {
            String[] idArray = ids.Split(',');
            int count = 0;
            foreach (String id in idArray)
            {
                if (!"".Equals(id))
                {
                    count += this.repository.DisableData(id);
                }
            }
            return count;
        }

        /// <summary>
        /// 根据分页条件获取分页菜单数据
        /// </summary>
        /// <param name="menuName">查询条件</param>
        /// <param name="pager"></param>
        /// <param name="direction">排序方式</param>
        /// <param name="sort">排序字段</param>
        /// <returns></returns>
        public Hashtable getTableListByPage(String cbCustPDLine, Pager pager, String direction, String sort, Boolean isPage)
        {
            string orderBy = "c.seq";
            return repository.getListByPage(pager.pageSize, pager.pageNo, orderBy, cbCustPDLine);
        }
           */
    }


}